const express = require('express')
const router = express.Router()
const db = require('../dataBase/db')

//获取信息
var list = (req, res) => {
    var sql = "SELECT * FROM `user`.`houses` LIMIT 0,1000"
    db.query(sql, (err, data) => {
        if (err) {
            res.json({ code: 500, msg: "失败" })
        } else {
            res.json({ code: 200, data })
        }
    })
}
router.get('/list', list);

//添加
var add = (req, res) => {
    var sql = "INSERT INTO `user`.`houses` (`title`, `description`, `price`, `area`, `bedrooms`, `bathrooms`, `address`, `city`, `state`, `zip_code`, `country`, `latitude`) VALUES ('3', '2323', 23, 2323, 23, 23, '23', '23', '23', '23', '23', 23)"
    db.query(sql, (err, data) => {
        if (err) {
            res.json({ code: 500, msg: "添加失败" })
        } else {
            res.json({ code: 200, data })
        }
    })
}
router.post('/add', add);


//删除
var del = (req, res) => {
    var sql = "DELETE FROM `user`.`houses` WHERE `house_id` = 4"
    db.query(sql, (err, data) => {
        if (err) {
            res.json({ code: 500, msg: "删除失败" })
        } else {
            res.json({ code: 200, data })
        }
    })
}
router.post('/del', del);


//修改
var UPD = (req, res) => {
    var sql = "UPDATE `user`.`houses` SET `title` = '修改的无敌大房子' WHERE `house_id` = 3"
    db.query(sql, (err, data) => {
        if (err) {
            res.json({ code: 500, msg: "修改失败" })
        } else {
            res.json({ code: 200, data })
        }
    })
}
router.post('/upd', UPD);


//查找
var SELECT = (req, res) => {
    var sql = "SELECT * FROM `user`.`houses` WHERE `house_id` = '1' LIMIT 0,1000"
    db.query(sql, (err, data) => {
        if (err) {
            res.json({ code: 500, msg: "查找失败" })
        } else {
            res.json({ code: 200, data })
        }
    })
}
router.post('/SELECT', SELECT);

module.exports = router;